#include "connectorwidget.h"
#include "ui_connectorwidget.h"
#include "qmath.h"
#include "qlist.h"

ConnectorWidget::ConnectorWidget(QSerialPort *_port,QWidget *parent) :
    m_port(_port),
    QWidget(parent),
    ui(new Ui::ConnectorWidget)
{
    ui->setupUi(this);
    //进入连接器自动搜索一次串口
    this->FindCOM();
    //连接器内其他数据默认选项

    /*设置串口配置默认参数*/
    ui->comboBox_COM->setCurrentIndex(0);
    m_port->setPortName(ui->comboBox_COM->currentText());
    ui->comboBox_Baud->setCurrentIndex(6);
    m_port->setBaudRate(QSerialPort::Baud19200);
    ui->comboBox_DataBit->setCurrentIndex(3);
    m_port->setDataBits(QSerialPort::Data8);
    ui->comboBox_ParityBit->setCurrentIndex(2);
    m_port->setParity(QSerialPort::NoParity);
    ui->comboBox_StopBit->setCurrentIndex(0);
    m_port->setStopBits(QSerialPort::OneStop);
}

/*端口搜索*/
int ConnectorWidget::FindCOM()
{
    ui->comboBox_COM->clear();
    foreach(const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
         m_port->setPort(info);
         if(info.isValid())
         {
             m_port->open(QIODevice::ReadWrite);
             ui->comboBox_COM->addItem(info.portName());
             m_port->close();
         }
         else
         {
             //所有 串口 检查 云内串口很多错误所以不检查，只显示能用的
//             QString errinfo = "port";
//             qDebug()<<info.description();
//             qDebug()<<info.portName();
//             errinfo += info.description();
//             errinfo += info.portName();
//             if(info.isBusy())
//                 //2263426
//                 errinfo += " is busy";
//             else if(info.isNull())
//                 errinfo += " is Null";
//             else if(!info.isValid())
//                 errinfo +=" is unValid";
//             else
//                 errinfo += "cannot open !";
//             qDebug()<<errinfo;
//             const QString tips = "提示信息";
//             QMessageBox::information(this,tips,errinfo);
         }
    }
    return 1;
}

ConnectorWidget::~ConnectorWidget()
{
    delete ui;
    delete m_port;
}

void ConnectorWidget::on_toolButton_Fresh_clicked()
{
    if(FindCOM())
        QMessageBox::information(this,"提示信息","端口检索成功！");
    else
        QMessageBox::information(this,"提示信息","端口检索失败！原因：QIODevice::ReadWrite无法打开");
}

/*设置端口参数*/
void ConnectorWidget::on_pushButton_OK_clicked()
{
    //查看输入框内参数
//    qDebug()<<"COMBOBOX_COM:"<<ui->comboBox_COM->currentText();
//    qDebug()<<"COMBOBOX_BAUD:"<<ui->comboBox_Baud->currentText();
//    qDebug()<<"COMBOBOX_DATABIT:"<<ui->comboBox_DataBit->currentText();
//    qDebug()<<"COMBOBOX_PARITYBIT:"<<ui->comboBox_ParityBit->currentText();
//    qDebug()<<"COMBOBOX_STOPBIT:"<<ui->comboBox_StopBit->currentText();

//    qDebug()<<"COMBOBOX_COM:"<<ui->comboBox_COM->currentIndex();
//    qDebug()<<"COMBOBOX_BAUD:"<<ui->comboBox_Baud->currentIndex();
//    qDebug()<<"COMBOBOX_DATABIT:"<<ui->comboBox_DataBit->currentIndex();
//    qDebug()<<"COMBOBOX_PARITYBIT:"<<ui->comboBox_ParityBit->currentIndex();
//    qDebug()<<"COMBOBOX_STOPBIT:"<<ui->comboBox_StopBit->currentIndex();

    //设置m_port
    //ui->comboBox_COM->setCurrentIndex(0);
    int flagSetSuccess = 0;
    bool setSuccess = false;

    m_port->setPortName(ui->comboBox_COM->currentText());

    setSuccess = m_port->setBaudRate(ui->comboBox_Baud->currentText().toInt());
    if(!setSuccess)
    {
        flagSetSuccess++;
    }
    switch (ui->comboBox_DataBit->currentIndex())
    {
        case 0:
            setSuccess = m_port->setDataBits(QSerialPort::Data5);
            if(!setSuccess)
            {
                flagSetSuccess++;
            }
            break;
        case 1:
            setSuccess = m_port->setDataBits(QSerialPort::Data6);
            if(!setSuccess)
            {
                flagSetSuccess++;
            }
            break;
        case 2:
            setSuccess = m_port->setDataBits(QSerialPort::Data7);
            if(!setSuccess)
            {
                flagSetSuccess++;
            }
            break;
        case 3:
            setSuccess = m_port->setDataBits(QSerialPort::Data8);
            if(!setSuccess)
            {
                flagSetSuccess++;
            }
            break;
        default:
            break;
    }

    switch(ui->comboBox_ParityBit->currentIndex())
    {
    case 0:
        setSuccess = m_port->setParity(QSerialPort::OddParity);
        if(!setSuccess)
        {
            flagSetSuccess++;
        }
        break;
    case 1:
        setSuccess = m_port->setParity(QSerialPort::EvenParity);
        if(!setSuccess)
        {
            flagSetSuccess++;
        }
        break;
    case 2:
        setSuccess = m_port->setParity(QSerialPort::NoParity);
        if(!setSuccess)
        {
            flagSetSuccess++;
        }
        break;
    default:
        break;
    }

    switch (ui->comboBox_StopBit->currentIndex()) {
    case 0:
        setSuccess = m_port->setStopBits(QSerialPort::OneStop);
       if(!setSuccess)
        {
           flagSetSuccess++;
       }
        break;
    case 1:
        setSuccess = m_port->setStopBits(QSerialPort::OneAndHalfStop);
        if(!setSuccess)
        {
            flagSetSuccess++;
        }
        break;
    case 2:
        setSuccess = m_port->setStopBits(QSerialPort::TwoStop);
        if(!setSuccess)
        {
            flagSetSuccess++;
        }
        break;
    case 3:
        setSuccess = m_port->setStopBits(QSerialPort::UnknownStopBits);
        if(!setSuccess)
        {
            flagSetSuccess++;
        }
    default:
        break;
    }

    //check m_port
    qDebug()<<"m_port_COM:"<<m_port->portName();
    qDebug()<<"m_port_BAUD:"<<m_port->baudRate();
    qDebug()<<"m_port_DATABIT:"<<m_port->dataBits();
    qDebug()<<"m_port_PARITYBIT:"<<m_port->parity();
    qDebug()<<"m_port_STOPBIT:"<<m_port->stopBits();

    if(flagSetSuccess == 0 && m_port->open(QIODevice::ReadWrite))
    {
        QMessageBox::information(this,"提示信息","端口打开成功");
        this->close();
    }
    else
    {
        QMessageBox::critical(this,"警告","端口打开失败！");
    }
}

void ConnectorWidget::on_pushButton_cancel_clicked()
{
    m_port->close();
}
